<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
*T_User
*Description: model manage users
*@author quanhm
*/

Class GCMModel extends MY_Model{
	
	function __construct() { 
		parent::__construct();

		$this->table_name = T_GCMUSER;
	}
    
    function getAllUsers(){
    	$this->db->select();
    	$this->db->from($this->table_name);
    	$this->db->join(T_USERS, T_USERS.'.'.USER_ID.' = '.$this->table_name.'.'.USER_ID , 'left' ) ;
    	$query_result = $this->db->get();

    	$query_result = $query_result->result_array();

    	return $query_result;
    }

    function getGCMUsers($where = -1){

        if(is_array($where)){
            $this->db->where($where);
        }else if(is_numeric($where) && $where != -1){
    		$this->db->where(LOGIN_ID , $where);
    	}

    	$this->db->select();
    	$this->db->from($this->table_name);
    	$this->db->join(T_USERS, T_USERS.'.'.USER_ID.' = '.$this->table_name.'.'.USER_ID , 'left' ) ;
    	$query_result = $this->db->get();

    	$query_result = $query_result->result_array();

    	return $query_result;
    }

    function storeNewUserGCM($regId , $userId , $type){
    	$this->storeUserGCM(array( GCM_REGID => $regId , USER_ID => $userId , $GCM_TYPE => $type ));
    }

    function storeUserGCM($array){
    	if($this->countRegIdAlreadyExist($array[GCM_REGID]) > 0){
    		return $this->updateInfoUserGCM($array);
    	}

    	$this->db->insert($this->table_name , $array);

    	//get last id
		$id_last = $this->db->insert_id();

		return $id_last;
    }

    /**
     * update data of table login
     * @param  $array : data update
     * @return integer : rows affected.
     */
    function updateInfoUserGCM($array = array()){
        if($array == null || !is_array($array)){
            return 0;
        }
    	$this->db->where(GCM_REGID , $array[GCM_REGID]);
    	$this->db->update($this->table_name , $array);
    	// return $this->db->affected_rows();
        return 1;
    }

    function countRegIdAlreadyExist($regId){
    	$this->db->where(GCM_REGID , $regId);
    	return $this->db->count_all_results($this->table_name);
    }
    function getRegIdListFromRegId($regId){
    	$result = $this->getGCMUsers(array(GCM_REGID => $regId ));

    	if(count($result) > 0){
    		$result = $result[0];
    	}else{
    		return 0;
    	}
    	return $this->getRegIdListFromUserId(-1 , $regId);

    }

    function getRegIdListFromUserId($user_id = -1, $regId = ''){
    	
        if($user_id != -1){
            $this->db->where(array(USER_ID => $user_id , GCM_ACTIVE=>1 ));    
        }
    		
    	
    	$query_result = $this->db->get($this->table_name);

    	$query_result = $query_result->result_array();

    	$gcm_user_array = array();
    	
    	if(strlen($regId ) > 0){
    		for($i = 0 ; $i < count($query_result) ; $i++){
	    		if(strcmp($regId , $query_result[$i][GCM_REGID]) != 0){
	    			$gcm_user_array[]	= $query_result[$i][GCM_REGID];
	    		}
	    	}	
    	}else{
    		for($i = 0 ; $i < count($query_result) ; $i++){
	    		$gcm_user_array[]	= $query_result[$i][GCM_REGID];	    		
	    	}
    	}
    	

    	return $gcm_user_array;
    }

    /**
     * update gcm table by property
     * @param  array  $data  [description]
     * @param  array  $where [description]
     * @return [type]        [description]
     */
    public function update_infor_gcm_by_property($data = array(), $where = array()){
        if($data == null || !is_array($data) || $where == null || !is_array($where)){
            return null;
        }

        $this->db->where($where);
        $this->db->update($this->table_name, $data);
        return $this->db->affected_rows();
    }

}